//
//  BuyOneByOneCourseVC.m
//  HTP
//
//  Created by admin on 15/11/11.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "BuyOneByOneCourseVC.h"
#import "MyOrderViewModel.h"
#import "MyFormViewController.h"
#import "PayOffOrderInfoViewController.h"

@interface BuyOneByOneCourseVC ()
@property (nonatomic,assign) NSInteger count;
@property (nonatomic,retain) UILabel *countLabel;
@property (nonatomic,retain) UILabel *allCostLabel;
@end

@implementation BuyOneByOneCourseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:RGBCOLOR(240, 240, 240, 1)];
    _count = 1;
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [self _initView];
    [self _initNav];
}
-(void)viewDidAppear:(BOOL)animated{
    
}
//TODO:UI绘制
-(void)_initView{
    if (self.view.subviews.count != 0) {
        return;
    }
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth / 2.0)];
    [backImage setBackgroundColor:[UIColor whiteColor]];
    [backImage setImage:[UIImage imageNamed:@"whitebg"]];
    [self.view addSubview:backImage];
    
    UILabel *nameLabel = [self getNormolLabel];
    [nameLabel setTextColor:RGBCOLOR(200, 200, 200, 1)];
    [nameLabel setFrame:CGRectMake(0, ScreenWidth / 4.0 - 45, ScreenWidth, 25)];
    nameLabel.text = [NSString stringWithFormat:@"%@1对1课程", _courseM.teacherName];
    [nameLabel setFont:[UIFont systemFontOfSize:18]];
    [self.view addSubview:nameLabel];
    
    UILabel *pricesLabel = [self getNormolLabel];
    [pricesLabel setFrame:CGRectMake(0, ScreenWidth / 4.0, ScreenWidth, 25)];
    [pricesLabel setText:[NSString stringWithFormat:@"%.2lf元/小时",[_courseM.tsPrice floatValue]]];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%.2lf元/小时",[_courseM.tsPrice floatValue]]];
    //设置：在0-3个单位长度内的内容显示成红色
    NSInteger length = [NSString stringWithFormat:@"%.2lf元/小时",[_courseM.tsPrice floatValue]].length;
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18] range:NSMakeRange(0, length - 6)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:NSMakeRange(0, length - 4)];
    pricesLabel.attributedText = str;
    [self.view addSubview:pricesLabel];
    
    UILabel *selectLabel = [self getNormolLabel];
    [selectLabel setText:@"选择课时(小时)"];
    [selectLabel setFrame:CGRectMake(0, ScreenWidth / 2.0 + (ScreenHeihgt - NavHe - 49 - ScreenWidth /2.0 - 170) / 2.0, ScreenWidth, 20)];
    [self.view addSubview:selectLabel];
    
    UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth / 2.0 - 10, CGRectGetMaxY(selectLabel.frame)+ 30 , 20, 20)];
    addBtn.contentMode = UIViewContentModeScaleAspectFit;
    [addBtn setBackgroundImage:[UIImage imageNamed:@"up"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addCountAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
    
    UIButton *reduceBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth / 2.0 - 10, CGRectGetMaxY(addBtn.frame) + 35 , 20, 20)];
    reduceBtn.contentMode = UIViewContentModeScaleAspectFit;
    [reduceBtn setBackgroundImage:[UIImage imageNamed:@"down"] forState:UIControlStateNormal];
    [reduceBtn addTarget:self action:@selector(reduceCountAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:reduceBtn];
    
    _countLabel = [self getNormolLabel];
    [_countLabel setFrame:CGRectMake(0, CGRectGetMaxY(addBtn.frame), ScreenWidth, 35)];
    [_countLabel setText:@"1"];
    [self.view addSubview:_countLabel];
    
    UIImageView *bottomImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, ScreenHeihgt - NavHe - 49, ScreenWidth, 49)];
    [bottomImage setImage:[UIImage imageNamed:@"pricebg"]];
    bottomImage.userInteractionEnabled = YES;
    [self.view addSubview:bottomImage];
    
    UIButton *contactBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 49 * 5.0 / 3.0, 49)];
    [contactBtn setBackgroundImage:[UIImage imageNamed:@"callme"] forState:UIControlStateNormal];
    [bottomImage addSubview:contactBtn];
    
    UIButton *payBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth - 80, 0, 80, 49)];
    [payBtn setTitle:@"立即支付" forState:UIControlStateNormal];
    [payBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [payBtn.titleLabel setTextAlignment:_TEXT_ALIGN_CENTER];
//    [payBtn.titleLabel setTextColor:[UIColor orangeColor]];
    [payBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [payBtn addTarget:self action:@selector(addOrder) forControlEvents:UIControlEventTouchUpInside];
    [bottomImage addSubview:payBtn];
    
    UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth - 81, 8, 1, 33)];
    [lineImage setImage:[UIImage imageNamed:@"line_s"]];
    [bottomImage addSubview:lineImage];
    
    _allCostLabel = [self getNormolLabel];
    _allCostLabel.textAlignment = _TEXT_ALIGN_LEFT;
    _allCostLabel.font = [UIFont systemFontOfSize:18];
    NSMutableAttributedString *strCost = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%.2lf",[_courseM.tsPrice floatValue]]];
    
    [strCost addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 1)];
    _allCostLabel.attributedText = strCost;
    [_allCostLabel setFrame:CGRectMake(CGRectGetMaxX(contactBtn.frame)+10, 0, ScreenWidth - 90 - CGRectGetMaxX(contactBtn.frame), 49)];
    [bottomImage addSubview:_allCostLabel];
}
-(void)_initNav{
    [self addCustomBackBase];
    [self addCustomTitleWithTitle:@"报名"];
}
//TODO:Action
/**
 *  添加订单
 */
-(void)addOrder{
    MyOrderViewModel *myOrderVM = [[MyOrderViewModel alloc] init];
    [myOrderVM setBlockWithReturnBlock:^(id returnValue) {
        [self dismissSVProgress];
        [self gotoOrderDetail:[NSString stringWithFormat:@"%@",returnValue[INTERFACE_INFO]]];
    } WithErrorBlock:^(id errorCode) {
        [self showSVProgressFailed:@"下单失败"];
    } WithFailureBlock:^{
        [self showSVProgressFailed:INTERFACE_FAILURE_CHIN];
    }];
    [self showSvprogress:@"生成订单"];
    [myOrderVM addOrderByParent:[USERCONTENT getUserParentId] withTeacherSetId:[NSString stringWithFormat:@"%@",_courseM.tsId] andConut:[NSString stringWithFormat:@"%ld",(long)_count]];
}
/**
 *  订单添加完成 转移至订单详情页面
 */
-(void)gotoOrderDetail:(NSString *)orderId{
    NSMutableArray *arrays = [NSMutableArray array];
    NSMutableArray *viewControllerArray = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [viewControllerArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            [arrays addObject:obj];
        }
    }];
    [arrays addObject:[[MyFormViewController alloc] init]];
    PayOffOrderInfoViewController *orderInfo = [[PayOffOrderInfoViewController alloc] init];
    orderInfo.orderID = orderId;
    [arrays addObject:orderInfo];
    [self.navigationController setViewControllers:arrays animated:YES];
//    OrderInfoViewController
}
/**
 *  数量加一
 */
-(void)addCountAction{
    _count ++;
    [_countLabel setText:[NSString stringWithFormat:@"%ld",(long)_count]];
    NSMutableAttributedString *strCost = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%.2lf",[_courseM.tsPrice floatValue]*_count]];
    [strCost addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 1)];
    _allCostLabel.attributedText = strCost;
}
/**
 *  数量减一
 */
-(void)reduceCountAction{
    if (_count == 1) {
        return;
    }
    _count --;
    [_countLabel setText:[NSString stringWithFormat:@"%ld",(long)_count]];
    NSMutableAttributedString *strCost = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"￥%.2lf",[_courseM.tsPrice floatValue]*_count]];
    //设置：在0-3个单位长度内的内容显示成红色
    [strCost addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, 1)];
    _allCostLabel.attributedText = strCost;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
